<template>
  <div class="article-grid-container">
    <div class="article-grid">
      <div 
        v-for="article in articles" 
        :key="article.articleId"
        class="grid-item"
      >
        <router-link :to="`/article/${article.articleId}`" class="grid-link">
          <div class="grid-image">
            <img 
              v-if="article.coverImage" 
              :src="article.coverImage" 
              :alt="article.title"
              loading="lazy"
            >
            <div v-else class="image-placeholder">
              <i class="iconfont icon-picture"></i>
            </div>
            <span v-if="article.isTop" class="top-badge">置顶</span>
          </div>
          <div class="grid-content">
            <h3 class="grid-title">{{ article.title }}</h3>
            <div class="grid-meta">
              <span class="meta-item">
                <i class="iconfont icon-eye"></i> {{ article.viewCount || 0 }}
              </span>
              <span class="meta-item">
                <i class="iconfont icon-time"></i> {{ formatDate(article.publishTime) }}
              </span>
            </div>
          </div>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ArticleGrid',
  props: {
    articles: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    formatDate(dateStr) {
      if (!dateStr) return '未知'
      const date = new Date(dateStr)
      return `${date.getMonth()+1}-${date.getDate()}`
    }
  }
}
</script>

<style scoped>
.article-grid-container {
  margin: 0 -10px;
}

.article-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.grid-item {
  background: #fff;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  transition: all 0.3s;
}

.grid-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.12);
}

.grid-link {
  display: block;
  text-decoration: none;
  color: #333;
}

.grid-image {
  position: relative;
  height: 0;
  padding-bottom: 60%;
  overflow: hidden;
}

.grid-image img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s;
}

.grid-item:hover .grid-image img {
  transform: scale(1.05);
}

.image-placeholder {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #c0c4cc;
  font-size: 36px;
}

.top-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(255, 153, 0, 0.8);
  color: #fff;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 12px;
}

.grid-content {
  padding: 12px;
}

.grid-title {
  font-size: 15px;
  margin: 0 0 8px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  height: 42px;
}

.grid-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #999;
}

.meta-item {
  display: flex;
  align-items: center;
}

.meta-item i {
  margin-right: 3px;
  font-size: 12px;
}

@media (max-width: 1200px) {
  .article-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 992px) {
  .article-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 576px) {
  .article-grid {
    grid-template-columns: 1fr;
  }
  
  .grid-item {
    max-width: 350px;
    margin: 0 auto;
  }
}
</style>